unit log_activity;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, ComObj, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxClasses, cxControls, cxGridCustomView,
  cxGridDBTableView, cxGrid, ZAbstractRODataset, ZAbstractDataset, Setting,
  ZDataset, ZConnection, cxGridExportLink, ExtCtrls, ZAbstractTable,
  ComCtrls, Grids;

type
  TlogActivityForm = class(TForm)
    cxGrid1: TcxGrid;
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1TableView1: TcxGridTableView;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    DataSource1: TDataSource;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    cbkabkot: TComboBox;
    cbkec: TComboBox;
    cbdesa: TComboBox;
    btnrefresh: TButton;
    btnexport: TButton;
    SaveDialog1: TSaveDialog;
    ZTable1: TZTable;
    ZQuery2: TZQuery;
    DateTimePicker1: TDateTimePicker;
    DateTimePicker2: TDateTimePicker;
    Label4: TLabel;
    Label5: TLabel;
    Panel2: TPanel;
    cxGrid1Level2: TcxGridLevel;
    cxGrid1Level1: TcxGridLevel;
    sgLog: TStringGrid;
    queryUser: TZQuery;
    procedure btnrefreshClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnexportClick(Sender: TObject);
    procedure cbkabkotChange(Sender: TObject);
    procedure cbkecChange(Sender: TObject);
    procedure cbdesaChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure AutoSizeCol(Grid: TStringGrid; Column: integer);
    function SaveAsExcelFile(stringGrid: TstringGrid; FileName: string): Boolean;
  private
    { Private declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; kode_login:string);overload;
  end;

var
  logActivityForm: TlogActivityForm;
  sloginkab,sloginkec:string;
  sKabkot:String;
  sKec:String;
  sDesa:String;
  skode:string;

implementation

{$R *.dfm}
constructor TlogActivityForm.Create(AOwner: TComponent; kode_login:string);
begin
  if(length(kode_login)>2)then
  begin
    sloginkab:=copy(kode_login,1,2);
    sloginkec:=copy(kode_login,3,3);
  end
  else
  begin
    sloginkab:=kode_login;
    sloginkec:='';
  end;

  inherited Create(AOwner);
end;

procedure TlogActivityForm.FormCreate(Sender: TObject);
var
  setting:TSetting_Form;
begin
//  BorderIcons := BorderIcons - [biMaximize];
  //showmessage(slogin);
  Setting := TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.Connected:=true;
  ZQuery1.Connection:=ZConnection1;
  ZQuery2.Connection:=ZConnection1;
  queryUser.Connection:=ZConnection1;
  Ztable1.Connection:=ZConnection1;
  sKabkot:='';
  sKec:='';
  sDesa:='';
  skode:='';

  datetimepicker1.Date := Date;
  datetimepicker2.Date := Date;

  sgLog.Cells[0,0]:='NO';
  sgLog.Cells[1,0]:='NIK';
  sgLog.Cells[2,0]:='NKK';
  sgLog.Cells[3,0]:='NAMA';
  sgLog.Cells[4,0]:='TEMPAT';
  sgLog.Cells[5,0]:='TANGGAL';
  sgLog.Cells[6,0]:='IBU';
  sgLog.Cells[7,0]:='KAWIN';
  sgLog.Cells[8,0]:='KELAMIN';
  sgLog.Cells[9,0]:='ALAMAT';
  sgLog.Cells[10,0]:='RW';
  sgLog.Cells[11,0]:='RT';
  sgLog.Cells[12,0]:='TPS';
  sgLog.Cells[13,0]:='GANDA';
  sgLog.Cells[14,0]:='USERNAME';
  sgLog.Cells[15,0]:='TANGGAL AKSI';
  sgLog.Cells[16,0]:='KETERANGAN';
  sgLog.Cells[17,0]:='ALASAN';
  sgLog.ColWidths[1] := 30;
  sgLog.ColWidths[1] := 100;
  sgLog.ColWidths[2] := 100;
  sgLog.ColWidths[3] := 150;
  sgLog.ColWidths[4] := 100;
  sgLog.ColWidths[5] := 70;
  sgLog.ColWidths[6] := 100;
  sgLog.ColWidths[7] := 50;
  sgLog.ColWidths[8] := 60;
  sgLog.ColWidths[9] := 150;
  sgLog.ColWidths[10] := 100;
  sgLog.ColWidths[11] := 40;
  sgLog.ColWidths[12] := 40;
  sgLog.ColWidths[13] := 50;
  sgLog.ColWidths[14] := 70;
  sgLog.ColWidths[15] := 120;
  sgLog.ColWidths[16] := 200;
  sgLog.ColWidths[16] := 200;



  btnRefresh.Enabled:=false;
  btnexport.Enabled:=false;

  cbkabkot.Text:='';
  cbkec.text:='';
  cbdesa.text:='';


  if(sloginkec<>'')then
  begin
    cbkabkot.enabled:=false;
    cbkec.enabled:=false;
    with ZTable1 do begin
      Active:=False;
      TableName:='kabkot';
      cbKabkot.Clear;
      filter:='KODE='+quotedstr(sloginkab);
      filtered:=true;
      Active:=True;
      cbkabkot.items.add(fieldbyname('TAMPIL').asstring);
      cbkabkot.itemindex:=0;
      sKabkot:=sloginkab;

      Active := False;
      TableName := 'kecamatan';
      Filter := 'KODE_KECAMATAN = '+QuotedStr(sloginkab+sloginkec);
      Filtered := True;
      Active := True;
      First;
      cbkec.clear;
      cbkec.Items.Add(FieldByName('TAMPIL').AsString);
      cbkec.itemindex:=0;
      skec:=sloginkab+sloginkec;

      Active := False;
      TableName := 'desa';
      Filter := 'KODE_KECAMATAN = '+QuotedStr(sloginkab+sloginkec);
      Filtered := True;
      Active := True;
      First;
      while (not Eof) do
      begin
        cbdesa.Items.Add(FieldByName('TAMPIL').AsString);
        Next;
      end;

    end;
  end
  else
  begin
    if(sloginkab <> '00') then
    begin
      cbkabkot.enabled:=false;
    end;
    with ZTable1 do begin
      Active:=False;
      TableName:='kabkot';
      cbKabkot.Clear;
      if (sloginkab='00') then begin
        Active:=True;
        while (not Eof) do begin
          cbkabkot.Items.Add(fieldbyname('TAMPIL').asstring);
          Next;
        end;
      end
      else
      begin
        Active:=True;
        filter:='KODE='+quotedstr(sloginkab);
        filtered:=true;
        cbkabkot.items.add(fieldbyname('TAMPIL').asstring);
        cbkabkot.itemindex:=0;
        sKabkot:=sloginkab;
        cbkabkot.Enabled:=false;

        Active := False;
        TableName := 'kecamatan';
        Filter := 'KODE_KABKOT = '+QuotedStr(sloginkab);
        Filtered := True;
        Active := True;
        First;
        while (not Eof) do
        begin
          cbkec.Items.Add(FieldByName('TAMPIL').AsString);
          Next;
        end;
      end;
    end;
  end;

  //showmessage('yeaye');
  //WindowState:=wsNormal;
end;

procedure TlogActivityForm.AutoSizeCol(Grid: TStringGrid; Column: integer);
var
  i, W, WMax: integer;
begin
  WMax := 0;
  for i := 0 to (Grid.RowCount - 1) do begin
    W := Grid.Canvas.TextWidth(Grid.Cells[Column, i]);
    if W > WMax then
      WMax := W;
  end;
  Grid.ColWidths[Column] := WMax + 5;
end;

procedure TlogActivityForm.btnrefreshClick(Sender: TObject);
var
  id_peny, id_diproses, proses_string, tanggal_mulai, tanggal_akhir : string;
  leng,idx,jumlah_history,no : integer;
  list_id : TStringList;
  tglBuf : TStrings;
  dari, sampai : Tdate;
  ada: boolean;
  i: integer;
begin
  sgLog.RowCount:=2;
  no:=1;
  ada:=false;
  idx:=0;
  list_id := TStringList.Create;
  dari := datetimePicker2.Date;
  sampai := datetimePicker1.Date;
  if(dari > sampai) then
    showMessage('Tanggal mulai harus lebih kecil dari tanggal akhir, silakan perbaiki.')
  else
  tanggal_mulai := datetostr(dari);
  tanggal_akhir := datetostr(sampai);

  tanggal_mulai := FormatDateTime('yyyy-mm-dd', dari);
  tanggal_akhir := FormatDateTime('yyyy-mm-dd', sampai);
  //showMessage(copy(tanggal_mulai,7,4)+'-'+copy(tanggal_mulai,1,2)+'-'+copy(tanggal_mulai,4,2));
  begin
  //showMessage(datetostr(dari));
  with zquery2 do begin
    active:=false;
    sql.Clear;
    sql.text:='SELECT ID FROM penyimpan WHERE NAMA LIKE '+QuotedStr('pemilih_'+sdesa);
    //showMessage('SELECT ID FROM penyimpan WHERE NAMA LIKE '+QuotedStr('pemilih_'+sdesa));
    active:=true;
    First;
    while(not eof) do begin
      id_peny:=fieldByName('ID').AsString;
      //showmessage('hosh');
      Next;
    end;
  end;

  //masukin id di history ke list_id
  with zquery1 do begin
    active:=false;
    sql.clear;
    sql.Text:= 'SELECT DISTINCT ID_USER FROM HISTORY WHERE ID_USER LIKE '+QuotedStr(id_peny+'-%');
    active:=true;
    First;
    while (not eof) do begin
      //showMessage('abc');
      list_id.Add(fieldByName('ID_USER').AsString);
      Next;
    end;
  end;

  leng := Length(id_peny);

  for idx:=0 to list_id.Count-1 do
  begin
    ada:=false;
    id_diproses := list_id.Strings[idx];
    //cari history nya
    proses_string:= 'SELECT a.*, b.KETERANGAN, c.NAMA AS NAMA_TPS FROM ((history a INNER JOIN keterangan_aksi b ON a.KET_AKSI = b.ID) LEFT JOIN daftar_tps c ON a.TPS = c.ID_TPS) WHERE TANGGAL_AKSI > '+QuotedStr(Tanggal_mulai+' 00:00:00')+' AND TANGGAL_AKSI < '+QuotedStr(tanggal_akhir+' 23:59:59')+' AND ID_USER LIKE '+quotedstr(id_diproses);
    //showMessage(proses_string);
    with queryUser do begin
      active:=false;
      sql.clear;
      sql.text:=proses_string;
      active:=true;
      First;
      jumlah_history:=0;
      while(not eof) do begin
        if(ada = false) then
          sgLog.RowCount := sgLog.RowCount +1;
        ada:=True;
        sgLog.RowCount := sgLog.RowCount +1;
        sgLog.Cells[1,sgLog.RowCount-1]:=fieldByName('NIK').AsString;
        sgLog.Cells[2,sgLog.RowCount-1]:=fieldByName('NKK').AsString;
        sgLog.Cells[3,sgLog.RowCount-1]:=fieldByName('NAMA').AsString;
        sgLog.Cells[4,sgLog.RowCount-1]:=fieldByName('TEMPAT').AsString;
        sgLog.Cells[5,sgLog.RowCount-1]:=fieldByName('TANGGAL').AsString;
        sgLog.Cells[6,sgLog.RowCount-1]:=fieldByName('IBU').AsString;
        sgLog.Cells[7,sgLog.RowCount-1]:=fieldByName('KAWIN').AsString;
        sgLog.Cells[8,sgLog.RowCount-1]:=fieldByName('KELAMIN').AsString;
        sgLog.Cells[9,sgLog.RowCount-1]:=fieldByName('ALAMAT').AsString;
        sgLog.Cells[10,sgLog.RowCount-1]:=fieldByName('RW').AsString;
        sgLog.Cells[11,sgLog.RowCount-1]:=fieldByName('RT').AsString;
        sgLog.Cells[12,sgLog.RowCount-1]:=fieldByName('NAMA_TPS').AsString;
        sgLog.Cells[13,sgLog.RowCount-1]:=fieldByName('GANDA').AsString;
        sgLog.Cells[14,sgLog.RowCount-1]:=fieldByName('USERNAME').AsString;
        sgLog.Cells[15,sgLog.RowCount-1]:=fieldByName('TANGGAL_AKSI').AsString;
        sgLog.Cells[16,sgLog.RowCount-1]:=fieldByName('KETERANGAN').AsString;
        sgLog.Cells[17,sgLog.RowCount-1]:=fields[16].asstring;
        jumlah_history := jumlah_history +1;
        next;
      end;
    end;

    if(ada=true) then begin
    //tampilkan aslinya
    proses_string:= 'SELECT a.*, b.NAMA AS NAMA_TPS FROM pemilih_'+sdesa+' a LEFT JOIN daftar_tps b ON a.TPS = b.ID_TPS WHERE ID_USER = '+copy(id_diproses,Length(id_peny)+2,Length(id_diproses));
    //showMessage(proses_string);
    with queryUser do begin
      active:=false;
      sql.clear;
      sql.text:=proses_string;
      active:=true;
      First;
      //jumlah_history := 0;
      while(not eof) do begin
        //showMessage(inttostr(sgLog.RowCount-2-jumlah_history));
        sgLog.Cells[0,sgLog.RowCount-1-jumlah_history]:=inttostr(no);
        sgLog.Cells[1,sgLog.RowCount-1-jumlah_history]:=fieldByName('NIK').AsString;
        sgLog.Cells[2,sgLog.RowCount-1-jumlah_history]:=fieldByName('NKK').AsString;
        sgLog.Cells[3,sgLog.RowCount-1-jumlah_history]:=fieldByName('NAMA').AsString;
        sgLog.Cells[4,sgLog.RowCount-1-jumlah_history]:=fieldByName('TEMPAT').AsString;
        sgLog.Cells[5,sgLog.RowCount-1-jumlah_history]:=fieldByName('TANGGAL').AsString;
        sgLog.Cells[6,sgLog.RowCount-1-jumlah_history]:=fieldByName('IBU').AsString;
        sgLog.Cells[7,sgLog.RowCount-1-jumlah_history]:=fieldByName('KAWIN').AsString;
        sgLog.Cells[8,sgLog.RowCount-1-jumlah_history]:=fieldByName('KELAMIN').AsString;
        sgLog.Cells[9,sgLog.RowCount-1-jumlah_history]:=fieldByName('ALAMAT').AsString;
        sgLog.Cells[10,sgLog.RowCount-1-jumlah_history]:=fieldByName('RW').AsString;
        sgLog.Cells[11,sgLog.RowCount-1-jumlah_history]:=fieldByName('RT').AsString;
        sgLog.Cells[12,sgLog.RowCount-1-jumlah_history]:=fieldByName('NAMA_TPS').AsString;
        sgLog.Cells[13,sgLog.RowCount-1-jumlah_history]:=fieldByName('GANDA').AsString;
        next;
      end;
    end;
    end;
    if(ada = true) then
    no:= no+1;
  end;
  for i := 0 to sgLog.ColCount - 1 do
      AutoSizeCol(sgLog, i);
  //showMessage(id_peny);

  {cxGrid1DBTableView1.DataController.Filter.Clear;
  cxGrid1DBTableView1.ClearItems;
  with zquery1 do begin
      active:=false;
      sql.clear;
      //sqsql.text:='SELECT a.USERNAME AS PELAKU, b.KETERANGAN AS AKSI, a.TANGGAL_AKSI AS WAKTU, '+'a.NIK,a.NKK,a.NAMA,a.ALAMAT,a.RT,a.RW, (SELECT kabkot.NAMA FROM penyimpan,kabkot WHERE MID(penyimpan.NAMA,9,2)=kabkot.KODE AND penyimpan.ID=LEFT(a.ID_USER,1)) AS kabkot,'+'(SELECT kecamatan.NAMA FROM penyimpan,kecamatan WHERE MID(penyimpan.NAMA,9,5)=kecamatan.KODE_KECAMATAN AND penyimpan.ID=LEFT(a.ID_USER,1)) AS KECAMATAN,(SELECT desa.NAMA FROM penyimpan,desa'+' WHERE desa.KODE_DESA='+quotedstr(sdesa)+' AND penyimpan.ID=LEFT(a.ID_USER,1)) AS KELURAHAN '+'FROM `history` a INNER JOIN `keterangan_aksi` b ON a.KET_AKSI = b.ID';
      //sql.text:='SELECT a.USERNAME AS PELAKU, b.KETERANGAN AS AKSI, a.TANGGAL_AKSI AS WAKTU, '+'a.NIK,a.NKK,a.NAMA,a.ALAMAT,a.RT,a.RW, (SELECT kabkot.NAMA FROM penyimpan,kabkot WHERE MID(penyimpan.NAMA,9,2)=kabkot.KODE AND penyimpan.ID=LEFT(a.ID_USER,1)) AS kabkot,'+'(SELECT kecamatan.NAMA FROM penyimpan,kecamatan WHERE MID(penyimpan.NAMA,9,5)=kecamatan.KODE_KECAMATAN AND penyimpan.ID=LEFT(a.ID_USER,1)) AS KECAMATAN,(SELECT desa.NAMA FROM penyimpan,desa'+' WHERE RIGHT(penyimpan.NAMA,8)=desa.KODE_DESA AND penyimpan.ID=LEFT(a.ID_USER,1)) AS KELURAHAN '+'FROM `history` a INNER JOIN `keterangan_aksi` b ON a.KET_AKSI = b.ID';
      sql.text:='SELECT a.USERNAME AS PELAKU, b.KETERANGAN AS AKSI, a.TANGGAL_AKSI AS WAKTU, '+'a.NIK,a.NKK,a.NAMA,a.ALAMAT,a.RT,a.RW, (SELECT kabkot.NAMA FROM penyimpan,kabkot WHERE MID(penyimpan.NAMA,9,2)=kabkot.KODE AND penyimpan.ID=LEFT(a.ID_USER,'+inttostr(leng)+')) AS kabkot,'+'(SELECT kecamatan.NAMA FROM penyimpan,kecamatan WHERE MID(penyimpan.NAMA,9,5)=kecamatan.KODE_KECAMATAN AND penyimpan.ID=LEFT(a.ID_USER,'+inttostr(leng)+')) AS KECAMATAN,(SELECT desa.NAMA FROM penyimpan,desa'+' WHERE RIGHT(penyimpan.NAMA,8)=desa.KODE_DESA AND penyimpan.ID=LEFT(a.ID_USER,'+inttostr(leng)+')) AS KELURAHAN '+'FROM `history` a INNER JOIN `keterangan_aksi` b ON a.KET_AKSI = b.ID where LEFT(ID_USER,'+inttostr(leng)+') in (Select ID from penyimpan where NAMA='+quotedstr('pemilih_'+sdesa)+')  ORDER BY a.ID_USER ASC';
      //showMessage('SELECT a.USERNAME AS PELAKU, b.KETERANGAN AS AKSI, a.TANGGAL_AKSI AS WAKTU, '+'a.NIK,a.NKK,a.NAMA,a.ALAMAT,a.RT,a.RW, (SELECT kabkot.NAMA FROM penyimpan,kabkot WHERE MID(penyimpan.NAMA,9,2)=kabkot.KODE AND penyimpan.ID=LEFT(a.ID_USER,'+inttostr(leng)+')) AS kabkot,'+'(SELECT kecamatan.NAMA FROM penyimpan,kecamatan WHERE MID(penyimpan.NAMA,9,5)=kecamatan.KODE_KECAMATAN AND penyimpan.ID=LEFT(a.ID_USER,'+inttostr(leng)+')) AS KECAMATAN,(SELECT desa.NAMA FROM penyimpan,desa'+' WHERE RIGHT(penyimpan.NAMA,8)=desa.KODE_DESA AND penyimpan.ID=LEFT(a.ID_USER,'+inttostr(leng)+')) AS KELURAHAN '+'FROM `history` a INNER JOIN `keterangan_aksi` b ON a.KET_AKSI = b.ID where LEFT(ID_USER,'+inttostr(leng)+') in (Select ID from penyimpan where NAMA='+quotedstr('pemilih_'+sdesa)+')  ORDER BY a.ID_USER ASC');
      active:=true;
   end;
   DataSource1.DataSet:=zquery1;
   cxGrid1DBTableView1.DataController.DataSource:=DataSource1;
   cxGrid1DBTableView1.DataController.CreateAllItems;
   }
   list_id.Free;
   end;
end;

procedure TlogActivityForm.btnexportClick(Sender: TObject);
begin
  if SaveDialog1.Execute then begin
    if SaveAsExcelFile(sglog, saveDialog1.FileName) then
      ShowMessage('Berhasil menyimpan');
    //ExportGridToExcel(saveDialog1.FileName,cxGrid1,False);
  end;
end;

procedure TlogActivityForm.cbkabkotChange(Sender: TObject);
var
  pilihan:String;
begin
  if (cbKabkot.ItemIndex > -1) then
  begin
    pilihan := cbKabkot.Items[cbKabkot.ItemIndex];
    with ZTable1 do
      begin
        Active := False;
        TableName := 'kabkot';
        Filter := 'TAMPIL = '+QuotedStr(pilihan);
        Filtered := True;
        Active := True;
        First;
        sKabkot := FieldByName('KODE').AsString;
        //cb2
        Active := False;
        TableName := 'kecamatan';
        Filter := 'KODE_KABKOT = '+QuotedStr(sKabkot);
        Filtered := True;
        Active := True;
        First;
      end;
    cbkec.Clear;
    cbDesa.Clear;
    while (not ZTable1.Eof) do
      begin
        cbkec.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
  end
    else
  begin
    sKabkot:='';
    cbkec.Clear;
    cbDesa.Clear;
  end;
  sKec:='';
  sDesa:='';
  btnRefresh.Enabled:=false;
  btnexport.Enabled:=false;

end;

procedure TlogActivityForm.cbkecChange(Sender: TObject);
var
  pilihan:String;
begin
  if (cbKec.ItemIndex > -1) then
  begin
    pilihan := cbKec.Items[cbKec.ItemIndex];
    ZTable1.Active := False;
    ZTable1.TableName := 'kecamatan';
    ZTable1.Filter := 'TAMPIL = '+QuotedStr(pilihan)+' AND KODE_KABKOT = '+quotedstr(skabkot);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    sKec := ZTable1.FieldByName('KODE_KECAMATAN').AsString;
    //cb3
    ZTable1.Active := False;
    ZTable1.TableName := 'desa';
    ZTable1.Filter := 'KODE_KECAMATAN = '+QuotedStr(sKec);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    cbdesa.Clear;
    while (not ZTable1.Eof) do
      begin
        cbdesa.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
  end
    else
  begin
    sKec:='';
    cbdesa.Clear;

  end;
  sDesa:='';
  btnRefresh.Enabled:=false;
  btnexport.Enabled:=false;

end;

procedure TlogActivityForm.cbdesaChange(Sender: TObject);
var
  desa:String;
begin
  if (cbdesa.ItemIndex > -1) then
  begin
    desa := cbdesa.Items[cbdesa.ItemIndex];
    //
    ZTable1.Active := False;
    ZTable1.TableName := 'desa';
    ZTable1.Filter := 'TAMPIL = '+QuotedStr(desa)+' AND KODE_KABKOT = '+QuotedStr(sKabkot)+' AND KODE_KECAMATAN = '+QuotedStr(sKec);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    sDesa := ZTable1.FieldByName('KODE_DESA').AsString;
    btnRefresh.Enabled:=true;
    btnexport.Enabled:=true;
    //buat
  end
    else
  begin
    sDesa:='';
    btnRefresh.Enabled:=false;
    btnexport.Enabled:=false;

  end;
end;

procedure TlogActivityForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
action:=caFree;
end;

function TlogActivityForm.SaveAsExcelFile(stringGrid: TstringGrid; FileName: string): Boolean;
const
  xlWBATWorksheet = -4167;
var
  Row, Col: Integer;
  GridPrevFile: string;
  XLApp, Sheet: OLEVariant;
  tanggal_mulai, tanggal_akhir : String;
begin
  Result := False;
  XLApp := CreateOleObject('Excel.Application');
  try
    tanggal_mulai := FormatDateTime('dd-mm-yyyy', dateTimePicker2.Date);
    tanggal_akhir := FormatDateTime('dd-mm-yyyy', dateTimePicker1.Date);
    XLApp.Visible := False;
    XLApp.Workbooks.Add(xlWBatWorkSheet);
    Sheet := XLApp.Workbooks[1].WorkSheets[1];
    Sheet.Name := 'Log Aktifitas';
    XLApp.WorkSheets[1].Range['A1:R1'].Merge;
    Sheet.Cells[1, 1] := 'Log Aktivitas data pemilih tanggal '+tanggal_mulai+' sampai '+tanggal_akhir;
    for col := 0 to stringGrid.ColCount - 1 do
      for row := 0 to stringGrid.RowCount - 1 do
        Sheet.Cells[row + 2, col + 1] := stringGrid.Cells[col, row];
        try
          XLApp.Workbooks[1].SaveAs(FileName);
          Result := True;
        except
// Error ?
        end;
        finally
        if not VarIsEmpty(XLApp) then
        begin
          XLApp.DisplayAlerts := False;
          XLApp.Quit;
          XLAPP := Unassigned;
          Sheet := Unassigned;
        end;
      end;
end;

end.
